//
//  ZCSetupVC.m
//  zctx
//
//  Created by apple on 16/1/4.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCSetupVC.h"
#import "ZCMeDataCell.h"
#import "ZCAboutVC.h"
#import "DZWebViewController.h"
@interface ZCSetupVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *viewFooter;
@end

@implementation ZCSetupVC



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCSetupVC"];
    [MTA trackPageViewBegin:@"ZCSetupVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCSetupVC"];
    [MTA trackPageViewEnd:@"ZCSetupVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initnavegation];
     self.navigationItem.title = @"设置";

    self.view.backgroundColor =[UIColor colorWithHexString:@"#ebeded"];

    [self initTableView]; //初始化UITableView
    //    [self initTableViewHeader]; //创建TableviewHeader
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCMeDataCell" bundle:nil] forCellReuseIdentifier:@"datacell"];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = self.viewFooter;
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    self.tableView.separatorColor = RGB(236, 237, 238);
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIndentifier = @"datacell";
    ZCMeDataCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[ZCMeDataCell alloc]initWithStyle:
                UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.titleLable.textColor = [UIColor colorWithHexString:@"#525252"];
    cell.titleLable.text = @"关于我们";
    cell.infoLable.hidden = YES;
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 0.000001;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCAboutVC *zcabout = [[ZCAboutVC alloc]init];
    zcabout.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:zcabout animated:YES];
    [self performSelector:@selector(unselectCell:) withObject:nil afterDelay:0.3];
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 10)];
    return headView;
}

-(void)unselectCell:(id)sender{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

- (UIView *)viewFooter {
    if (!_viewFooter) {
        _viewFooter = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
        
        UIButton *userbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        [userbutton setFrame:CGRectMake(50, 24, SCREEN_WIDTH-100, 20)];
        userbutton.titleLabel.font = [UIFont systemFontOfSize:13];
        [userbutton setTitleColor:[UIColor colorWithHexString:@"#03ab9e"] forState:UIControlStateNormal];
        [userbutton setTitle:@"《用户服务协议》" forState:UIControlStateNormal];
        [userbutton addTarget:self action:@selector(userButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.viewFooter addSubview:userbutton];
        
        //提交
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.layer.cornerRadius = 5;
        [button setFrame:CGRectMake(20, 60, SCREEN_WIDTH-40, 40)];
        button.backgroundColor =[ UIColor colorWithHexString:@"#03ab9e"];
        [button setTitle:@"退出登录" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(dismissLogin) forControlEvents:UIControlEventTouchUpInside];
        [self.viewFooter addSubview:button];
    }
    return _viewFooter;
}

-(void)userButtonClick{
    
    DZWebViewController *vc = [[DZWebViewController alloc] init];
    vc.strURL = [NSString stringWithFormat:@"%@%@",[NetworkManager strURLBasic],@"/index.php/M/Index/agreement"];
    vc.title = @"用户服务协议";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)dismissLogin{
    if (DEVICE_SYSTEM_VERSION) {
        [self showAlert];;
    }else{
        [self showAlertold];
    }
}

-(void)showAlert{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否退出" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // 回调在block里面
        // to do..
        NSLog(@"sd");
    }];
    UIAlertAction *actionDelete = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // to do..
        NSLog(@"sfg");
        [self dismiss];
    }];
    [alert addAction:action];
    [alert addAction:actionDelete];
    [self presentViewController:alert animated:YES completion:nil];
}


-(void)showAlertold{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"退出登录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex ==1) {
        [self dismiss];
    }
}


-(void)dismiss{
    NSLog(@"退出程序");
    [UserModel setMid:@"0"];
    [UserModel setMobile:@"0"];
    [UserModel setPassword:@"0"];
    [UserModel setUsername:@"0"];
    [UserModel setUsername:nil];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"login" object:@{@"login":@"1"}];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
